$(function(){
  /*banner的轮播*/
  var Banner = new Swiper('#banner', {
    observer: true,
    observeParents: true,
    effect: 'fade',
    loop: true,
    speed: 3000,
    autoplay: {
      delay: 500,
      stopOnLastSlide: false,
      disableOnInteraction: true,
    },
    pagination: {
      el: '.swiper-pagination',
      clickable: true,
      renderBullet: function (index, className) {
        return '<li class="' + className + '"><div class="change-width"></div></li>';
      }
    }

  });
  var local=getUrlParam('locale')==null?window.basicLocale:getUrlParam('locale');
  if(local!=null&&local!=''){
      var locals=local.split('_');
      if(locals[0]=='zh'){
          $(".cn").addClass('active');
          $(".en").removeClass('active');
      }else if(locals[0]=='en'){
          $(".cn").removeClass('active');
          $(".en").addClass('active');
      }else{
          $(".cn").addClass('active');
          $(".en").removeClass('active');
      }
  }else{
      $(".cn").addClass('active');
      $(".en").removeClass('active');
  }
  //中英文语言切换
//  $('.lang a').on('click', function(){
//    $(this).addClass('active').siblings().removeClass('active');
//  });
  $('#userName').on('blur', function(){
	 if($(this).val()==''){
		 $('#tip').text($.i18n.prop('ztron.login.required.username'));
		 $('#tip').show();
	 }
  }).on('focus', function(){
	  $('#tip').text('');
	  $('#tip').hide();
  });
  $('#password').on('blur', function(){
	 if($(this).val()==''){
		 $('#tip').text($.i18n.prop('ztron.login.required.password'));
		 $('#tip').show();
	 }
  }).on('focus', function(){
	  $('#tip').text('');
	  $('#tip').hide();
  });
});

$('body').keydown(function(event){
	event.returnValue=false;
	onKeyPress(event);
});
function validateForm(){
	var userName = $("#userName").val();
	var password = $("#password").val();
	if(userName==''){
		$('#tip').text($.i18n.prop('ztron.login.required.username'));
		$('#tip').show();
	} else if(password==''){
		$('#tip').text($.i18n.prop('ztron.login.required.password'));
		$('#tip').show();
	} else {
		loginSubmit();
	}
}

function loginSubmit() {
	var userName = $("#userName").val();
	var passWord = $("#password").val();
	if( $("#notMatch").val()=="1"){

        return;
	}
	$.ajax({
	    async: false,
	    cache: false,
	    type: 'POST',
	    dataType: "json",
	    data: {"userName": userName, "password": $.md5(passWord),"locale": window.locale_header},
	    url: window.baseUrl + 'sys/login',//请求的action路径
	    success: function (res) {
	    	if (res.code == 0) {
	    		//请求成功后，写入 access_token
	    		var storage = window.localStorage;
	    		storage.access_token = res.token;
	    		storage.userName = userName;
	    		window.localStorage.showMessageBox="true";
	    		if(res.IsOAuth){
	    			location.href = window.baseUrl+"oauth/authorize?client_id="+res.client_id+"&redirect_uri="+res.redirect_uri+"&locale="+window.locale_header;
	    		}else{
	    			location.href = "http://119.91.255.117:19000/homeIndex";
	    		}
	    	} else {
	    		$('#tip').text(res.msg);
	    		$('#tip').show();
	    	}
	    },
	    error: function () {//请求失败处理函数
	    	$('#tip').text($.i18n.prop("baseInfo.login_failure_username_error"));
	    	$('#tip').show();
	    }
	});
}
function onKeyPress(e) {
	var keyCode = null;
	if (e.which)
	    keyCode = e.which;
	else if (e.keyCode)
	    keyCode = e.keyCode;
	if (keyCode == 13) {
	    $("#login_btn").trigger('click');
	    return false;
	}
	if(keyCode==9){
	    if(window.currentInput=='userName'){
	        window.currentInput='password';
	        setTimeout(function(){
	        	$("#password").focus();
	        },50);
	    }else if(window.currentInput=='password'){
	        window.currentInput='userName';
	        setTimeout(function(){
	        	$("#userName").focus();
	        },50);
	    }
	}
	return true;
}

